{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "9f2acbee",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/llm/gradient_base_model.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf9f19f3",
   "metadata": {},
   "source": [
    "# Gradient Base Model"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "9ea1fcaa",
   "metadata": {},
   "source": [
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7294e8a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install llama-index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79a726c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index --quiet\n",
    "%pip install gradientai --quiet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c2b0d5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"GRADIENT_ACCESS_TOKEN\"] = \"{GRADIENT_ACCESS_TOKEN}\"\n",
    "os.environ[\"GRADIENT_WORKSPACE_ID\"] = \"{GRADIENT_WORKSPACE_ID}\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a602a2a",
   "metadata": {},
   "source": [
    "## Flow 1: Query Gradient LLM directly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4baffaa2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms import GradientBaseModelLLM\n",
    "\n",
    "llm = GradientBaseModelLLM(\n",
    "    base_model_slug=\"llama2-7b-chat\",\n",
    "    max_tokens=400,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7039a65",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = llm.complete(\"Can you tell me about large language models?\")\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1112e828",
   "metadata": {},
   "source": [
    "## Flow 2: Retrieval Augmented Generation (RAG) with Gradient LLM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cacff36a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index import VectorStoreIndex, SimpleDirectoryReader, ServiceContext\n",
    "from llama_index.embeddings import LangchainEmbedding\n",
    "from langchain.embeddings import HuggingFaceEmbeddings"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f0afcf64",
   "metadata": {},
   "source": [
    "#### Download Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41e45271",
   "metadata": {},
   "outputs": [],
   "source": [
    "!mkdir -p 'data/paul_graham/'\n",
    "!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1edd41d1",
   "metadata": {},
   "source": [
    "### Load Documents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5941151",
   "metadata": {},
   "outputs": [],
   "source": [
    "documents = SimpleDirectoryReader(\"./data/paul_graham/\").load_data()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7df4407f",
   "metadata": {},
   "source": [
    "### Configure Gradient LLM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dec73a6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "embed_model = LangchainEmbedding(HuggingFaceEmbeddings())\n",
    "service_context = ServiceContext.from_defaults(\n",
    "    chunk_size=1024, llm=llm, embed_model=embed_model\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a131a8e",
   "metadata": {},
   "source": [
    "### Setup and Query Index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9b10269",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = VectorStoreIndex.from_documents(\n",
    "    documents, service_context=service_context\n",
    ")\n",
    "query_engine = index.as_query_engine()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac73eb65",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = query_engine.query(\n",
    "    \"What did the author do after his time at Y Combinator?\"\n",
    ")\n",
    "print(response)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('.venv': venv)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  },
  "vscode": {
   "interpreter": {
    "hash": "5ae9fa2777630f93d325d67fd0c37f7375ed1afcb20dd85f425eb8692a47ff3f"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
